/*
###############################################################################################
#                                                                                             # 
# The Page is used for Registering Site Users from the Admin Control Panel. This Page is      #
# created inside the Admin Control Panel and only the Administrator has a right to Add        #
# the new Site Users.                                                                         #
# The data will be used from the tbl_Users table                                              #
#     Author : Jaspal Mehta                                                                   #
#     Dated  : 16-Feb-2006                                                                    #
#                                                                                             #
###############################################################################################
*/

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SiteManager;

public partial class Admin_Ad_AddHospital : System.Web.UI.Page
{
    //Create an Object of Manager which is defined in the namespace SiteManager
    Manager mgr = new Manager(true, "ConnectionString");
    public int id = 0;
    public string path = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            id = Convert.ToInt32(Request.QueryString["id"]);
        }
        catch { id = 0; }
        //Label1 will be responsible to show the message whether the User is adding a new
        // User or Editing a Site user.
        ViewState["id"] = id;
        Label1.Font.Bold = true;
        Label1.ForeColor = System.Drawing.Color.Black;
        Label1.Text = id == 0 ? "Add Hospital" : "Edit Hospital";


        if (!IsPostBack)
        {
            //Function called 

            FillCombos(id);


        }
    }

    /// <summary>
    /// this function fills up all the drop downs
    /// </summary>
    /// <param name="id"></param>
    public void FillCombos(int id)
    {
        mgr.BindList("select distinct country from tbl_usa_hospitals", "country", "country", ddlCountry, "select");

        //   mgr.BindList("select countrycode,countryname from tbl_country", "countryname", "countrycode", combocountryid, "select");
        //   mgr.BindList("select secquestion,sno from tbl_secquestions", "secquestion", "sno", combosecretquestion, "select");
        ////   mgr.BindList("select healthcplan,sno from tbl_healthcplan", "healthcplan", "sno", combohealthplan, "select");
        //    mgr.BindList("select id,name from tbl_InsuranceCompanies", "name", "Id", combohealthplan, "select");
        //Edit a Site Users info
        if (id > 0)
        {
            // Create an Object of Recordset i.e Dataset 
            DataSet ds = mgr.ExecuteQuery("select * from tbl_usa_hospitals where Hospital_Name !='' and ID='" + id + "' ", "tbl_usa_hospitals");
            if (ds.Tables[0].Rows.Count > 0)
            {
                txthospitalname.Text = ds.Tables[0].Rows[0]["Hospital_Name"].ToString();
                txtCountry.Text = ds.Tables[0].Rows[0]["Country"].ToString();

               // mgr.BindList("select distinct country from tbl_usa_hospitals", "country", "country", ddlCountry, "");
                string country = ds.Tables[0].Rows[0]["Country"].ToString();
                if (!string.IsNullOrEmpty(country))
                {
                    ddlCountry.SelectedValue = ds.Tables[0].Rows[0]["Country"].ToString();
                    txtCountry.Text = ddlCountry.SelectedValue.ToString();
                }
                else
                {
                    mgr.BindList("select distinct country from tbl_usa_hospitals", "country", "country", ddlCountry, "select");
                }
                

                txtPhone.Text = ds.Tables[0].Rows[0]["Phone"].ToString();
                txtFax.Text = ds.Tables[0].Rows[0]["Fax"].ToString();


                txtWebsite.Text = ds.Tables[0].Rows[0]["Website"].ToString();
                txtEmail.Text = ds.Tables[0].Rows[0]["Email"].ToString();
                txtAbout.Text = ds.Tables[0].Rows[0]["About"].ToString();

                txtaddress.Text = ds.Tables[0].Rows[0]["Address"].ToString();

                if (ds.Tables[0].Rows[0]["latest"].ToString() == "False"
                    && ds.Tables[0].Rows[0]["featured"].ToString() == "False"
                    && ds.Tables[0].Rows[0]["tophospital"].ToString() == "False")
                {
                    chkstatus.SelectedValue = "0";
                }
                else
                {
                    if (ds.Tables[0].Rows[0]["featured"].ToString() == "True")
                    {
                        chkstatus.SelectedValue = "1";
                    }
                    if (ds.Tables[0].Rows[0]["latest"].ToString() == "True")
                    {
                        chkstatus.SelectedValue = "2";
                    }
                    if (ds.Tables[0].Rows[0]["tophospital"].ToString() == "True")
                    {
                        chkstatus.SelectedValue = "3";
                    }
                }
            }
            else
            {
                Label2.Visible = true;
                Label2.ForeColor = System.Drawing.Color.Red;
                Label2.Text = "No Records found!!";
                return;
            }
        }

    }



    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Label2.Text = "";
        //checking for duplicate email when inserting a new record                  
        string sql = "";

        int id = (int)ViewState["id"];

       


        //Retrieving All the Values of Forms in the Variables
        string hospitalname = txthospitalname.Text.Replace("'", "''").Trim();
        string Country = txtCountry.Text.Replace("'", "''").Trim();
       // string Country = ddlCountry.SelectedValue.ToString().Replace("'", "''").Trim();
        string Phone = txtPhone.Text.Replace("'", "''").Trim();
        string Fax = txtFax.Text.Replace("'", "''").Trim();
        string Website = txtWebsite.Text.Replace("'", "''").Trim();
        string Email = txtEmail.Text.Replace("'", "''").Trim();
        string About = txtAbout.Text.Replace("'", "''").Trim();
        string address = txtaddress.Text.Replace("'", "''").Trim();
        int latest = 0, tophospital = 0, featured = 0;

        if (chkstatus.SelectedValue != "0")
        {
            if (chkstatus.SelectedValue == "1")
            {
                featured = 1;
            }
            if (chkstatus.SelectedValue == "2")
            {
                latest = 1;
            }
            if (chkstatus.SelectedValue == "3")
            {
                tophospital = 1;
            }
        }

        DateTime tregdt = DateTime.Now;


        string sqlQuery = "";
        if (id > 0)
        {
            //Update Site User Query
            sqlQuery = "UPDATE [tbl_usa_hospitals] SET [Hospital_Name] = '" + hospitalname + "',[Country]='";
            sqlQuery += Country +"',[Phone] = '";
            sqlQuery += Phone + "',[Fax] = '" + Fax + "',[Website]='";
            sqlQuery += Website + "',[Email]='" + Email + "',[About]='" + About + "',[Address]='";
            sqlQuery += address + "',[latest] = '" + latest.ToString() + "',[featured] = '" + featured.ToString() + "',[tophospital] = '" + tophospital.ToString() + "' where Id=" + id;
            Response.Write("query" + sqlQuery);
        }
        else
        {
            //Insert Site User Query
            sqlQuery = "INSERT INTO [tbl_usa_hospitals] ([Hospital_Name],[Country],[Phone],[Fax],[Website],[Email],[About],[Map],[Address],[latest],[featured],[tophospital]) values('";
            sqlQuery += hospitalname + "','"+Country+"','" + Phone + "','" + Fax + "','" + Website + "','" + Email + "','" + About + "','','" + address + "','" + latest.ToString() + "',";
           // sqlQuery += hospitalname + "','USA','" + Phone + "','" + Fax + "','" + Website + "','" + Email + "','" + About + "','','" + address + "','" + latest.ToString() + "',";
            sqlQuery += " '" + featured.ToString() + "','" + tophospital.ToString() + "')";
            Response.Write("New User Added Successfully !!");
        }
        try
        {
            mgr.ExecuteQuery(sqlQuery);
            //If Query is successfull the User will be redirected to the main page
            //string page = Request.QueryString["from"].ToString();
            if (!string.IsNullOrEmpty(Request.QueryString["from"]))
            {
                if (Request.QueryString["from"].ToString() == "top")
                {
                    Response.Redirect("Ad_ManageTopHospitals.aspx");
                }
                if (Request.QueryString["from"].ToString() == "latest")
                {
                    Response.Redirect("Ad_ManageLatestHospitals.aspx");
                }
                if (Request.QueryString["from"].ToString() == "featured")
                {
                    Response.Redirect("Ad_ManageFeaturedHospitals.aspx");
                }
            }
            else
            {
                Response.Redirect("Ad_ManageFeaturedHospitals.aspx");
            }
        }
        // If any Exception occurs like if a System throws any exception at the 
        // time of running a query than it will be caught by the Mother class of 
        // Exception i.e 'Throwable'for which object has been created with a name of 'es'.
        catch (Exception es)
        {
            Response.Write(es.ToString());
            Label2.Visible = true;
            Label2.Font.Bold = true;
            Label2.ForeColor = System.Drawing.Color.Red;
            Label2.Text = es.ToString();
        }

    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        int id = (int)ViewState["id"];
        //If Cancel button is clicked than the user will be redirected to the main page.
        if (id > 0)
        {
            if (Request.QueryString["from"].ToString() == "top")
            {
                Response.Redirect("Ad_ManageTopHospitals.aspx");
            }
            if (Request.QueryString["from"].ToString() == "latest")
            {
                Response.Redirect("Ad_ManageLatestHospitals.aspx");
            }
            if (Request.QueryString["from"].ToString() == "featured")
            {
                Response.Redirect("Ad_ManageFeaturedHospitals.aspx");
            }
        }
        else
        {
            Response.Redirect("ADadminAccess.aspx");
        }
    }
    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtCountry.Text = ddlCountry.SelectedValue.ToString();
    }
}
